<!--教室管理-->
<template>
  <div class="class_management_container">
    <div class="search_box">
      <!-- v-if="powerList.add.show == 1" -->
      <div  class="btn btn_add" @click="showPopup('ruleForm')">
        <i class="el-icon-plus"></i>
        <span class="btn_txt">新建教室</span>
      </div>
      <div class="fr" >
        <el-form :model="searchForm" :inline="true" class="searchForm"  >

          <el-form-item size="small" style=" margin-right: 0;">
            <el-input placeholder="请输入教室编号/教室名称/校区" style="width: 260px;" size="small" v-model="searchForm.searchValue" @keyup.enter.native="searchData(searchValue)">
              <div slot="append" class="btn_search" @click="searchData(searchValue)">查询</div>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="clear"></div>
    </div>
    
    <div class="table_box">
      <adminTable :tableHead="tableHead" :tableData="tableData"></adminTable>
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.curPage"
        :page-sizes="[10, 20, 50]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.totalRows">
      </el-pagination>
    </div>

    <transition name="el-fade-in-linear">
      <div class="popup-container" v-show="popupState.showStatus">
        <div class="popup">
          <i class="el-icon-error icon_close" @click="popupState.showStatus = false"></i>
          <div class="title">{{popupState.popupName}}</div>

          <div class="content">
            <el-form :model="formData" ref="ruleForm" :disabled="popupState.popupName == '教室详情'"  :inline="true" :rules="rules" label-position="left"
               class="form">
              <div style="overflow:hidden">
                
                <el-form-item label="选择校区 : " prop="schoolId" size="small" class="">
                  <el-select @change="changeSchool" :disabled="popupState.popupName=='编辑教室'" v-model="formData.schoolId" placeholder="请选择校区">
                    <el-option v-for="(item,key) in schoolList" :key="key" :label="item.schoolName" :value="item.schoolId">
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="教室名称 : " prop="classRoomName" size="small" class="">
                  <el-input v-model="formData.classRoomName"  autocomplete="off" placeholder="请输入教室名称"></el-input>
                </el-form-item>

               
                <el-form-item label="面积(㎡) : " prop="classRoomArea" size="small" class="not_must">
                  <el-input  v-model="formData.classRoomArea" autocomplete="off" placeholder="请输入教室面积"></el-input>
                </el-form-item>

                
              </div>

            </el-form>

            <div class="btn_box">
              <div class="btn btn_add" v-if="popupState.popupName == '新建教室'" @click="submitAdd('ruleForm')">
                <span class="btn_txt">保存</span>
              </div>

              <div class="btn btn_add" v-if="popupState.popupName == '编辑教室'" @click="submitEdit('ruleForm')">
                <span class="btn_txt">保存</span>
              </div>
            </div>

          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<style>
.class_management_container .popup .school_list .school_item .el-form-item--small.el-form-item  {
  margin-bottom: 0;
  margin: 15px 0;
}
.class_management_container .popup .school_list .school_item .el-form-item--small.el-form-item label {
  width: 60px !important;
}

.class_management_container .popup .el-input--small {
  width: 100%;
}
.class_management_container .popup .el-form-item--mini.el-form-item, 
.class_management_container .popup .el-form-item--small.el-form-item {
  margin-right: 20px;
  width: 30.5%;
  margin-bottom: 25px;
}

/* .class_management_container .el-form-item__label {
  float: left;
} */
.class_management_container .popup .el-form-item__content {
  width: calc(100% - 90px)
}
</style>

<style scoped>

.class_management_container .btn_add i {
  margin-right: 5px;
  margin-left: 10px;
}

.icon_box{
  display: inline-block;
  line-height: 32px;
}
.el-icon {
  cursor: pointer;
  color: #77A4FF;
}
.school_list .school_item {
  background: #F0F2F5;
  padding: 0 0 0 20px !important;
  box-sizing: border-box;
  margin-bottom: 10px;;
}

.school_list .school_item 

.btn_box {
  margin-top: 20px;
}
.popup .content {
  padding-top: 30px;
}
</style>

<script>
import adminTable from '@/components/assembly/adminTable'
import {
  createNamespacedHelpers
} from "vuex";
let {
  mapState,
  mapGetters,
  mapMutations,
  mapActions
} = createNamespacedHelpers("classRoomManagement");
export default {
  name: 'classRoomManagement',
  data() {
    return {
      powerList: {
        add: {
          show: 0,
          menuId: ''
        },
        export: {
          show: 0,
          menuId: '',
        }
      },
      classId: '',
      popupState: {
        showStatus: false,
        popupName: '新建教室',
      },
      page: {
        curPage: 0,
        pageSize: 0,
        totalRows: 0,
      },
      searchForm: {
        searchValue: '',
      },
      formData: {
        schoolId: '',
        classRoomName: '',
        classRoomArea: '',
      },
      searchValue: '',

      tableHead: {
        // border: true,
        operation_width: '150px',
        setFunction: [
          {
            showState: "0",
            hideKey: "showUpdate",
            fun: this.tableEdit,
            name: "编辑"
          },
          {
            showState: "0",
            hideKey: "showDelete",
            fun: this.tableDelete,
            name: "删除"
          },
        ],
        data: [
          {
            name: '教室名称',
            key: 'classRoomName',
            align: 'center',
          },
          {
            name: "所属校区",
            key: "schoolName",
            align: "center",
          },
          {
            name: "面积(㎡)",
            key: "classRoomArea",
            align: "center"
          },
        ]
      },
      tableData: [],

      schoolList: [],
      powerSchoolList: [],
      schoolName: '',

      rules: {
        schoolId: [
          { required: true, message: '请选择校区', trigger: 'change' },
        ],
        classRoomName: [
          { required: true, message: '请输入教室名称', trigger: 'blur' },
        ],
      }
    }
  },
  created() {
    this.jwtAuth = JSON.parse(sessionStorage.getItem('jwtAuth'));
    this.queryAllSchool();
    
    
  },
  
  
  methods: {
    ...mapMutations([
      "updateInfo",
    ]),
    ...mapActions([
      "formGet",
      "post"
    ]),

    showPopup(formName) {
      let btnId = this.powerList.add.menuId;
      this.queryAllSchool(btnId);

      this.popupState = {
        showStatus: true,
        popupName: '新建教室',
      };
      this.$refs[formName].resetFields();   // 初始化
      
    },
    searchData(searchValue) {
      this.page.curPage = 1;
      this.initData(searchValue)
    },

    initData(searchForm) {
      // console.log(searchValue)
      let searchValue = searchForm || this.searchForm || '';
      let data = {}
      Object.assign(data, searchValue);
      data.curPage = this.page.curPage;
      data.pageSize = this.page.pageSize;
      data.accountId = this.jwtAuth.accountId;
      data.menuId = this.$route.name;
      

      let schoolIdArr = [];
      if(!data.schoolId || data.schoolId.length == 0) {
        this.schoolList.forEach(item => {
          schoolIdArr.push(item.schoolId);
        })
        data.schoolId = schoolIdArr;
      }else{
        schoolIdArr.push(this.searchForm.schoolId)
      }

      data.schoolId = schoolIdArr;

      console.log(this.searchForm.schoolId);
      console.log(data.schoolId);
      

      this.post({
        API: 'selectBmClassRoomPageList',
        data: data
      }).then(res => {
        console.log(res);
        this.tableData = res.data;
        // if(res.buttons) {
        //   res.buttons.menuList[0].children.forEach(item => {
        //     if(item.label == '新建教室') {
        //       this.powerList.add.show = 1
        //       this.powerList.add.menuId = item.menuId
        //     }
            
        //     console.log(this.powerList)
        //   })
        // }

        /**
          res.data.forEach(item => {
            if(item.buttonMap) {
              let menuList = item.buttonMap.menuList[0].children
              menuList.forEach((menuItem, menuIndex) => {
                if(menuItem.menuId == '1710') {     // 查看
                  if(menuItem.isDisplay == 1) {
                    item.showDetail = 1;
                  }else{
                    item.showDetail = 0;
                  }
                }
                if(menuItem.menuId == '1711') {     // 编辑
                  if(menuItem.isDisplay == 1) {
                    item.showUpdate = 1;
                  }else{
                    item.showUpdate = 0;
                  }
                }
                if(menuItem.menuId == '1712') {     // 删除
                  if(menuItem.isDisplay == 1) {
                    item.showDelete = 1;
                  }else{
                    item.showDelete = 0;
                  }
                }
              })
            }
          })
         */

        this.page = res.page;
      }).catch(err => {
        this.$message.error(err);
      })
    },

    queryAllSchool(btnId) {
      let accountId
      this.jwtAuth.schoolId == '' ? accountId = '' : accountId = this.jwtAuth.accountId
      
      this.post({    // 校区
        API: 'queryAllSchool',
        data: {
          accountId: accountId,
          menuId: btnId || this.$route.name,
          schoolIds: this.jwtAuth.schoolId.split(','),
        }
      }).then(res => {
        res.forEach((item, index) => {
          if(item.schoolId == '') {
            res.splice(index, 1);
          }
        })
        if(btnId) {
          this.powerSchoolList = res
        }else{
          this.schoolList = res
          // this.initData();
        }

        this.initData();

        // this.searchForm.schoolId = this.schoolList[0].schoolId
      })
    },

    // 新增提交
    submitAdd(formName) {
      let data = {}
      Object.assign(data, this.formData)

      data.schoolName = this.schoolName;
      
      console.log(data)
      // return false;

      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.post({
            API: 'insert',
            data: data
          }).then(res => {
            if(res == 1) {
              this.$message.success('新建成功')
              this.initData();
              this.popupState.showStatus = false;
            }
          }).catch(err => {
            this.$message.error(err);
          })
        } else {
          return false;
        }
      });

      
    },
    // 表格编辑 回显
    tableEdit(rowData) {
      
      this.classId = rowData.row.classRoomId;
      let data = {
        id: this.classId,
      }
      this.formGet({
        API: 'selectById',
        data: data
      }).then(res => {
        this.popupState = {
          popupName: '编辑教室',
          showStatus: true,
        }
        this.$refs['ruleForm'].resetFields();
        this.formData = res;
        
        
      }).catch(err => {
        this.$message.error(err);
      })
    },
    // 编辑提交
    submitEdit(rowData) {

      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          let data = this.formData
          data.schoolName = this.schoolName;

          this.post({
            API: 'update',
            data: data
          }).then(res => {
            this.$message.success('修改成功');
            this.initData();
            this.popupState.showStatus = false;
          }).catch(err => {
            this.$message.error(err);
          })
        }
      });
    },

    

    // 表格删除
    tableDelete(rowData) {
      let classId = rowData.row.classRoomId;
      let data = {
        id: classId,
      }

      this.$confirm('是否删除该教室?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.formGet({
          API: 'deleteById',
          data: data
        }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.initData();
        }).catch(err => {
          this.$message.error(err);
        })
        
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });

      
      
    },

    // 更改校区
    changeSchool(e) {
      this.schoolList.forEach(item => {
        if(e == item.schoolId) {
          this.schoolName = item.schoolName;
        }
      })

    },
    

    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.page.pageSize = val;
      this.initData();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page.curPage = val;
      this.initData();
    }
    
  },
  computed: {
    ...mapState([
    ]),
    // ...mapGetters(["selectedIds"])
  },
  components: {
    adminTable,
  },
}

</script>